package com.example.testapiauto.testCase.openApi;

import com.alibaba.fastjson.JSONObject;
import com.example.testapiauto.app.serviceImp.myAssertImp;
import com.example.testapiauto.modules.testCase.utils.HttpClient;
import com.example.testapiauto.yuxiang.authorization;
import com.example.testapiauto.yuxiang.resultsUtils;
import io.qameta.allure.*;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @author yuxiang
 * @version 1.0
 * @date 2021/4/23 15:58
 */

@Epic("获取页面按钮权限")
@Feature("获取")
public class getPageButtonAuthorization {

    private static final Logger logger = LogManager.getLogger(getPageButtonAuthorization.class);
    String testUrl = resultsUtils.TestUrl;
    String Token = authorization.TestEnvAuthenticationGET();
    myAssertImp myassertimp = new myAssertImp();
    String getToken = resultsUtils.token;



    @Story("用户菜单列表")
    @Test(description = "正常查看")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_1() {
        Object[] expect = new Object[2];
        expect[0] = "20000";
        expect[1] = "success";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token="+getToken+"&pageId=1";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：1，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，token为空")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_2() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token=&pageId=1";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：2，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，token为null")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_3() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token=null&pageId=1";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：3，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，token为错误的")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_4() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token=123&pageId=1";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：4，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，pageId为空")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_5() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token="+getToken+"&pageId=";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：5，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，pageId为null")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_6() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token="+getToken+"&pageId=null";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：6，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }

    @Story("用户菜单列表")
    @Test(description = "异常查看，pageId为错误的")
    @Description("验证是否能查看")
    @Owner("醉生")
    @Step("输入 url:" + resultsUtils.TestUrl)
    @Link(resultsUtils.TestUrl)
    @Link(name = "测试地址", type = "接口测试")
    @Severity(SeverityLevel.NORMAL)
    public void case_7() {
        Object[] expect = new Object[2];
        expect[0] = "20002";
        expect[1] = "business exception";
        String url = testUrl + "/openApi/privilege/v1/menuManage/getResourceIds?token="+getToken+"&pageId=jsw";
        logger.info("请求url：" + url);
        Map<String, Object> headers = new HashMap<>();
        headers.put("Authorization",Token);
        logger.info("请求头："+headers);
        String response = HttpClient.doGet_header(url, headers);
        logger.info("用例数：7，响应结果：" + response);
        JSONObject act = JSONObject.parseObject(response);
        act.get("code");
        act.get("msg");
        String[] JsonPach = new String[2];
        JsonPach[0] = "$.code";
        JsonPach[1] = "$.msg";
        boolean ResponseAssert = myassertimp.AssertByJsonPath(expect, act, JsonPach);
        logger.info("响应断言：" + ResponseAssert);
        Assert.assertEquals(expect[0], act.getString("code"));
    }
}
